package app;

import obj.Market;
import util.Globals;


public class Simulation {
	// Main Application Class
	public static Market mkt;
	
	public static void main (String[] args) {
		/** INITIALIZE */
		// get argument (config file)
		String configFile = setConfigFile(args);
		// load parameter and set app global settings
		Globals.loadGlobals(configFile);
		if (Globals.debug) { System.out.println("initate market (new Market())"); }
		mkt = new Market();
		if (Globals.debug) {
			mkt.reportDetails(-1);
		}
		run();
	}
	
	private static void run() {
		if (Globals.debug) { System.out.println("run/start market"); }
		for (int t = 0; t < Globals.periods; t++) {
			if (Globals.debug) { System.out.println("----- Period: " + t + " -----"); }
			if (Globals.debug) { 
				System.out.println("reporting market details (beginning of period:" + t + ")");
				mkt.reportDetails(t); 
			} else { mkt.report(t, ""); }
			
			if (Globals.debug) { System.out.println("execute market (period: " + t + ")"); }
			mkt.execute(); // make all orgs execute current strategy
			
			if (Globals.debug) { System.out.println("plan market (period: " + t + ")"); }
			mkt.plan(); // make all orgs plan their move
			
//			mkt.report(t, "before replace\t");
			if (Globals.debug) { System.out.println("replace market (period: " + t + ")"); }
			mkt.replace(); 

//			mkt.report(t, "after  replace\t");
//			mkt.report(t, "");
			if (Globals.debug) { 
				System.out.println("reporting market details (end of period:" + t + ")");
				mkt.reportDetails(t); 
			} 
		}
	}
	
	
	private static String setConfigFile(String[] args) {
		String retString = "";
		if (args.length > 1) {
			System.out.println("Need at most one argument (configuration file).  Try again.");
			System.exit(0);
		} else if (args.length == 0) {
			retString = "parameter.txt";
		} else {
			retString = args[0];
		}
		return retString;
	}
}